package cn.tworice.cryption.util;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.Security;
import java.util.Base64;

public class SMUtils {
    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    /**
     * 生成Base64格式的SM3哈希
     */
    public static String hashBase64(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("SM3", "BC");
            byte[] hashBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));
            return Base64.getEncoder().encodeToString(hashBytes);
        } catch (Exception e) {
            throw new RuntimeException("SM3哈希计算失败", e);
        }
    }
}
